<template>
    <section>
      <div class="filter-container">
          <common-input @keyup.enter.native="handleFilter" class="w200" placeholder="小店名称" v-model="listQuery.keyword" />
          <common-input @keyup.enter.native="handleFilter" class="w200 ml10" placeholder="用户" v-model="listQuery.memberKeyword" />
          <!-- <el-date-picker
            class="ml10"
            size="mini"
            v-model="listQuery.contractEnd"
            type="date"
          format="yyyy年MM月dd日"
          value-format="yyyy-MM-dd"
            placeholder="注册时间">
          </el-date-picker> -->
          <el-button size="mini" class="ml10" type="primary" icon="el-icon-search" @click="handleFilter">搜索</el-button>
          <el-button size="mini" class="ml10" @click="handleCreate" type="primary" icon="el-icon-plus">新增</el-button>
      </div>
      <el-tabs size="mini" :before-leave='leaveTab' v-model="activeName" type="card" @tab-click="handleClick">
        <el-tab-pane label="全部" name="0"></el-tab-pane>
        <el-tab-pane label="已审核" name="1"></el-tab-pane>
        <el-tab-pane label="待审核" name="2"></el-tab-pane>
        <el-tab-pane label="被驳回" name="3"></el-tab-pane>
      </el-tabs>
      <el-table max-height="500"
                  :data="list"
                  style="width: 100%">
              <el-table-column align="left" label="小店名称">
                  <template slot-scope="scope">
                      <span>{{scope.row.name}}</span>
                  </template>
              </el-table-column>
              <el-table-column align="left" label="所属用户" width="150">
                  <template slot-scope="scope">
                      <span>{{scope.row.memberName}}（{{scope.row.memberMobile}}）</span>
                  </template>
              </el-table-column>
              <el-table-column align="left" label="地区" width="150">
                  <template slot-scope="scope">
                      <span>{{scope.row.province}}{{scope.row.city}}{{scope.row.county}}</span>
                  </template>
              </el-table-column>
              <el-table-column align="right" label="余额">
                  <template slot-scope="scope">
                      <span>￥{{scope.row.money}}</span>
                  </template>
              </el-table-column>
              <el-table-column align="center" label="联系人">
                  <template slot-scope="scope">
                      <span>{{scope.row.consignee}}</span>
                  </template>
              </el-table-column>
              <el-table-column align="left" label="地址">
                  <template slot-scope="scope">
                      <span>{{scope.row.address}}</span>
                  </template>
              </el-table-column>
              <el-table-column align="center" label="注册时间" width="180">
                  <template slot-scope="scope">
                      <span>{{scope.row.createdAt}}</span>
                  </template>
              </el-table-column>
              <el-table-column align="center" label="状态">
                  <template slot-scope="scope">
                      <el-tag :type="getStatusColor(scope.row)">{{scope.row.statusText}}</el-tag>
                  </template>
              </el-table-column>
              <el-table-column
                      fixed="right"
                      label="操作"
                      width="100">
                  <template slot-scope="scope">
                      <el-dropdown>
                          <el-button
                                  type="text">
                              操作<i class="el-icon-arrow-down el-icon--right"></i>
                          </el-button>
                          <el-dropdown-menu slot="dropdown">
                              <el-dropdown-item @click.native="openShopDetail(scope.row.id)">详情</el-dropdown-item>
                              <el-dropdown-item @click.native="handleUpdate(scope.row)">编辑</el-dropdown-item>
                              <el-dropdown-item v-if="scope.row.status==0" @click.native="handleCheck(scope.row)">审核</el-dropdown-item>
                              <!-- <el-dropdown-item v-if="scope.row.status==1" @click.native="handleCheck2(scope.row)">取消审核</el-dropdown-item> -->
                              <!-- <el-dropdown-item v-if="scope.row.status==0&&scope.row.isFront==1" @click.native="handleRefuse(scope.row)">拒绝审核</el-dropdown-item> -->
                              <!-- <el-dropdown-item @click.native="handleRecharge(scope.row)">充值</el-dropdown-item> -->
                              <el-dropdown-item @click.native="handleDelete(scope.row)">删除</el-dropdown-item>
                          </el-dropdown-menu>
                      </el-dropdown>
                  </template>
              </el-table-column>
          </el-table>
          <div class="pagination-container">
              <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="listQuery.page" :page-sizes="[20,30, 50]" :page-size="listQuery.num" layout="total, sizes, prev, pager, next, jumper" :total="total"> </el-pagination>
          </div>
  
          <el-dialog title="商家充值" :visible.sync="dialogFormVisible" :close-on-click-modal="false" width="500px">
              <el-form :model="rechargeForm" ref="rechargeForm" label-width="150px">
                  <el-form-item label="充值金额" prop="money">
                      <el-input type="number" placeholder=""  v-model="rechargeForm.money" maxlength="10" />
                  </el-form-item>
                  <el-form-item label="上传凭证" prop="voucherImg">
                      <pic-sel style="float:left" v-model="rechargeForm.voucherImg" />
                  </el-form-item>
                  <el-form-item label="备注" prop="note">
                      <common-input type="textarea" :autosize="{ minRows: 3, maxRows: 5}" placeholder="" maxlength="100" v-model="rechargeForm.note" />
                  </el-form-item>
              </el-form>
              <div slot="footer" class="dialog-footer">
                  <el-button size="mini" @click="dialogFormVisible=false">取消</el-button>
                  <el-button :loading="rechargeLoading" size="mini" type="primary" @click="okRecharge()">确定</el-button>
              </div>
          </el-dialog>
  
          <el-dialog title="小店审核" :visible.sync="dialogCheckFormVisible" :close-on-click-modal="false" width="500px" @close="dialogCheckFormVisible = false">
              <el-form :model="checkForm" ref="checkForm" label-width="150px">
                  <el-form-item label="操作类型" prop="status">
                      <el-radio-group v-model="checkForm.status">
                          <el-radio :label="1">通过</el-radio>
                          <el-radio :label="2">驳回</el-radio>
                      </el-radio-group>
                  </el-form-item>
                  <el-form-item label="备注" prop="note">
                      <common-input type="textarea" :autosize="{ minRows: 3, maxRows: 5}" placeholder="拒绝审核时必填" maxlength="100" v-model="checkForm.note" />
                  </el-form-item>
              </el-form>
              <div slot="footer" class="dialog-footer">
                  <el-button size="mini" @click="dialogCheckFormVisible=false">取消</el-button>
                  <el-button :loading="checkLoading" size="mini" type="primary" @click="check('checkForm')">确定</el-button>
              </div>
          </el-dialog>
    </section>
  </template>
  
  <script>
      import {
        shopPageList
      } from '../../api/isp';
      import {
          seller_list,
          seller_del,
          seller_check,
          seller_check2,
          seller_refuse,
          seller_recharge
      } from '../../api/store';
  export default {
      components: {
          'pic-sel': () => import('../../components/PicSel/index')
      },
    data() {
      return {
          checkLoading: false,
          checkForm: {
              status: 1,
              note: undefined,
              id: 0,
          },
          dialogCheckFormVisible: false,
          dialogFormVisible: false,
          rechargeLoading: false,
          list: null,
          listLoading: true,
          listQuery: {
              page: 1,
              num: 20,
              keyword: undefined,
              status: '',
              contractEnd: undefined,
              memberKeyword: undefined,
              partnerId:sessionStorage.getItem("LOGIN_USERNAME_id")
          },
          total: null,
          activeName: '0',
          rechargeForm: {
              sellerId: 0,
              money: undefined,
              note: undefined,
          }
      };
    },
    mounted() {
    },
          created() {
              this.getList();
          },
  
    methods: {
      getList() {
          this.listLoading = true;
          shopPageList(this.listQuery).then(response => {
              this.list = response.data.list;
              this.total = response.data.total;
              this.listLoading = false;
          })
      },
         //打开小店详情
         openShopDetail(id) {
        //   router.push({path:'/'})
        //跳转小店详情
        // store.dispatch("setDetail", data);
        this.$router.push({ path: "./shoplistDetail", query: { shopid: id } });
      },
      handleFilter() {
          this.listQuery.page = 1;
          this.getList();
      },
      handleSizeChange(val) {
          this.listQuery.num = val;
          this.getList();
      },
      handleCurrentChange(val) {
          this.listQuery.page = val;
          this.getList();
      },
      handleClick(tab, event) {
        this.listQuery.status = this.activeName;
        this.handleFilter();
        // console.log(this.activeName)
      },
      leaveTab(activeName, oldActiveName) {
        // console.log(activeName)
        // return false;
      },
      handleCreate() {
        this.$router.push("/company/shop_create") 
      },
      handleUpdate(row) {
        this.$router.push("/company/shop_edit?id=" + row.id) 
      },
      getStatusColor(row) {
          if(row.status === 1) {
              return "success"
          }
  
          return "danger"
      },
      handleDelete(row) {
          this.$confirm('此操作将永久删除, 是否继续?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
          })
              .then(() => {
                  seller_del(row.id)
                      .then(() => {
                          this.$message.success("小店删除成功");
                          const index = this.list.indexOf(row);
                          this.list.splice(index, 1);
                      });
              }).catch(()=>{});
      },
      handleCheck(row) {
          this.checkForm.id = row.id;
          this.checkForm.status = 1;
          this.checkForm.note = "";
  
          this.dialogCheckFormVisible = true;
      },
      handleCheck2(row) {
          this.$confirm('确定取消审核?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
          })
              .then(() => {
                  seller_check2(row.id)
                      .then(() => {
                          this.$message.success("小店取消审核成功");
                          this.getList();
                      });
              }).catch(()=>{});
      },
      handleRefuse(row) {
          this.$confirm('确定拒绝审核?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
          })
              .then(() => {
                  seller_refuse(row.id)
                      .then(() => {
                          this.$message.success("小店拒绝审核成功");
                          this.getList();
                      });
              }).catch(()=>{});
      },
      handleRecharge(row) {
          this.rechargeForm.sellerId = row.id;
          this.rechargeForm.money = undefined;
          this.rechargeForm.note = undefined;
  
          this.dialogFormVisible = true;
      },
      okRecharge() {
          if(typeof this.rechargeForm.money == undefined) {
              this.$message.error("请输入充值金额");
          }
  
          let money = $.trim(this.rechargeForm.money);
          if(money.length < 1) {
              this.$message.error("请输入充值金额");
          } else {
              money = Number(money) || 0;
  
              if(money == 0) {
                  this.$message.error("请输入充值金额");
              } else {
                  this.rechargeLoading = true;
                  seller_recharge(this.rechargeForm).then((re)=>{
                      this.rechargeLoading = false;
                      this.$message.success("成功充值");
                      this.dialogFormVisible = false;
                      this.getList();
                  }).catch((err)=>{
                      this.rechargeLoading = false;
                  })
              }
          }
      },
              check(formName) {
                  this.checkForm.note = $.trim(this.checkForm.note)
  
                  // console.log(this.form)
                  if(this.checkForm.status == 2) {
                      if(this.checkForm.note.length < 1) {
                          this.$message.error("拒绝审核时备注必填");
                          return;
                      }
                  }
                  this.checkLoading = true;
                  seller_check(this.checkForm).then((res)=>{
                      this.checkLoading = false;
                      this.$message.success("小店已审核");
                      this.dialogCheckFormVisible = false;
                      this.getList();
                  }).catch((err)=>{
                      this.checkLoading = false;
  
                  })
              }
    }
  };
  </script>
  